<template>
  <div class="orderSend">
    <el-form ref="sendForm" :model="formItem" label-width="110px" @submit.native.prevent>
      <el-form-item label="选择类型：">
        <el-radio-group v-model="formItem.type" @change="changeRadioType('sendForm')">
          <el-radio label="0">发货</el-radio>
          <el-radio label="1">送货</el-radio>
          <el-radio label="2">无需配送</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="formItem.type==='0'">
        <el-form-item label="快递公司：" prop="expressId" :rules="{ required: true, message: '请选择快递公司', trigger: 'change' }">
          <el-select v-model="formItem.expressId" filterable size="small" placeholder="请选择快递公司" style="width:80%;">
            <el-option v-for="item in express" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号：" prop="deliveryNum" :rules="{ required: true, message: '请输入快递单号/手机号', trigger: 'blur' }">
          <el-input v-model="formItem.deliveryNum" size="small" placeholder="请输入快递单号/手机号" style="width:80%;"></el-input>
        </el-form-item>
      </template>
      <template v-else-if="formItem.type==='1'">
        <el-form-item label="送货人：" prop="deliveryId" :rules="{ required: true, message: '请选择送货人', trigger: 'change' }">
          <el-select v-model="formItem.deliveryId" filterable size="small" placeholder="请选择送货人" style="width:80%;">
            <el-option v-for="item in express" :key="item.id" :value="item.id" :label="`${item.name}（${item.mobile}）`"></el-option>
          </el-select>
        </el-form-item>
      </template>
      <template v-else-if="formItem.type==='2'">
        <el-form-item label="备注:" prop="fictitiousContent">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入要备注的内容" v-model="formItem.fictitiousContent" style="width:80%;"></el-input>
        </el-form-item>
      </template>
    </el-form>

    <div style="text-align: right;">
      <el-button size="small" type="primary" @click="sendSub('sendForm')">提交</el-button>
      <el-button size="small" @click="handleCancel('sendForm')">取消</el-button>
    </div>
  </div>
</template>

<script>
import {
  getLogisticsList as getLogisticsListApi,
  getDeliveryClerkList as getDistriClerkApi
} from '@/api/setting'
import {
  putOrderSendApi
} from '@/api/order'


export default {
  name: 'OrderSend',
  props: {
    order: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      formItem: {
        type: '0',
        expressId: '',
        deliveryId: '',
        deliveryNum: '',
        fictitiousContent: ''
      },
      express: [],
      delivery: []
    }
  },
  watch: {
    'formItem.type': {
      handler(newV) {
        if (newV == 0 && this.express.length == 0) {
          this.getLogistics()
        } else if (newV == 1 && this.delivery.length == 0) {
          this.getDelivery()
        }
      },
      immediate: true
    }
  },
  methods: {
    changeRadioType(formName) {
      this.$refs[formName].resetFields()
    },
    /**
     * 送货人
     */
    getDelivery() {
      getDistriClerkApi({
        orderBy: 'Id desc',
        pageNumber: 1,
        pageSize: 30,
        isshow: 1
      }).then(async res => {
        this.express = res.data
      })
    },
    /**
     * 物流公司列表
     */
    getLogistics() {
      getLogisticsListApi({
        orderBy: 'Sort desc',
        pageNumber: 1,
        pageSize: 30,
        isshow: 1
      }).then(async res => {
        this.express = res.data
      })
    },
    // 提交
    sendSub(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let obj = {}
          if (this.formItem.type == 0) {
            obj = {
              deliveryType: Number(this.formItem.type),
              expressId: this.formItem.expressId,
              deliveryNum: this.formItem.deliveryNum
            }
          } else if (this.formItem.type == 1) {
            obj = {
              deliveryType: Number(this.formItem.type),
              deliveryId: this.formItem.deliveryId
            }
          } else if (this.formItem.type == 2) {
            obj = {
              deliveryType: Number(this.formItem.type),
              fictitiousContent: this.formItem.fictitiousContent
            }
          }

          putOrderSendApi(this.order, obj).then(async res => {
            this.$message.success('发送货成功!')
            this.$refs[formName].resetFields()
            this.$emit('getSend')
            this.$emit('close')
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped lang="scss">
  .slot_footer {
    text-align: right;
  }
</style>
